<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: awfigq(awfigq@qq.com)
// +----------------------------------------------------------------------

// 抽奖
class LotteryAction extends CommonAction
{
	public function index()
	{
		$goods_id = intval($_REQUEST['goods_id']);
		$sn = $_REQUEST['sn'];
		$user_name = $_REQUEST['user_name'];
		$pname = $_REQUEST['pname'];
		
		$sql = "select a.*, u1.user_name, u1.mobile_phone, u1.email, u2.user_name as pname from ".C("DB_PREFIX")."lottery_no as a ".
			   " left outer join ".C("DB_PREFIX")."user u1 on u1.id = a.user_id ".
			   " left outer join ".C("DB_PREFIX")."user u2 on u2.id = a.invite_user_id where 1=1 ";
		if($goods_id > 0)
		{
			  $sql .= " and a.goods_id = ".$goods_id;
			  //$map['goods_id'] = $goods_id;
		}
			  
		if (!empty($sn)){
			$sql .= " and a.sn = '".$sn."'";
			//$map['sn'] = $sn;
		}
		
		if (!empty($user_name)){
			$sql .= " and u1.user_name like '%".$user_name."%'";
			//$map['user_name'] = array("like",'%'.$user_name.'%');
		}
		if (!empty($pname)){
			$sql .= " and u2.user_name like '%".$pname."%'";
			//$map['user_name'] = array("like",'%'.$user_name.'%');
		}
		
		$model = D ("LotteryNo");
		$this->_Sql_list($model,$sql,'','a.sn');
		
		$this->assign("sn",$_REQUEST['sn']);
		$this->assign("goods_id",$goods_id);
		$this->assign("pname",$pname);
		$this->assign("user_name",$_REQUEST['user_name']);
		$this->display ();
	}
	
	public function add()
	{
		$goods_id = intval($_REQUEST['goods_id']);
		$this->assign("goods_id",$goods_id);
		
		$this->display ();
	}
	
	public function insert()
	{

		$model = D("LotteryNo");
		
		if (false === $data = $model->create ()) {
			$this->error ( $model->getError () );
		}		
		
		$data['goods_id'] = intval($_POST['goods_id']);
		$data['order_id'] = intval($_POST['order_id']);
		$data['sn'] = trim($_POST['sn']);
		$data['user_id'] = intval($_POST['user_id']);
		$data['invite_user_id'] = intval($_POST['invite_user_id']);
		$data['status'] = 0;
		$data['invite_time'] = localStrToTime($_REQUEST['invite_time']);
		
		
		$this->assign ('goods_id', $data['goods_id'] );
		if($model->add($data))
		{
			$this->success (L('ADD_SUCCESS'));
		}
		else
		{
			$this->error($model->getError().$model->getLastSql());
		}
	}
	
	public function edit()
	{
		$model = D("LotteryNo");
		$id = $_REQUEST [$model->getPk ()];
		$vo = $model->getById($id);
		//u(
		$this->assign ( 'goods_id', $vo['goods_id'] );
		$this->assign ( 'vo', $vo );
		$this->display ();
	}
	
	public function update() {
		$model = D("LotteryNo");
		$data = $model->create ();
		if (false === $data) {
			$this->error ( $model->getError () );
		}
		// 更新数据
		$data['invite_time'] = localStrToTime($_REQUEST['invite_time']);
		
		$list=$model->save ($data);
		if (false !== $list) {
			//成功提示
			$this->saveLog(1,$list,$msg);	
			$this->success (L('EDIT_SUCCESS'));
		} else {
			//错误提示
			$this->saveLog(0,0,$msg);	
			$this->error (L('EDIT_FAILED'));
		}
	}
	
	public function foreverdelete()
	{
		//删除指定记录
		$model = D("LotteryNo");
		if (! empty ( $model )) {
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			
			if (isset ( $id )) {
				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
				if (false !== $model->where ( $condition )->delete ())
				{
					$this->saveLog(1);
					$this->success (L('DEL_SUCCESS'));
				} else {
					$this->saveLog(0);
					$this->error (L('DEL_FAILED'));
				}
			} else {
				$this->saveLog(0);
				$this->error ( L('INVALID_OP') );
			}
		}
		$this->forward ();
	}
	
	//导出会员列表
	function exportcsv($page = 1,$condition=''){
		set_time_limit(0);
		$limit = (($page - 1)*500).",".(500);
		$goods_id = intval($_REQUEST['goods_id']);
		$sn = $_REQUEST['sn'];
		$user_name = $_REQUEST['user_name'];
		$pname = $_REQUEST['pname'];
		
		$sql = "select a.*, u1.user_name, u1.mobile_phone, u1.email, u2.user_name as pname from ".C("DB_PREFIX")."lottery_no as a ".
			   " left outer join ".C("DB_PREFIX")."user u1 on u1.id = a.user_id ".
			   " left outer join ".C("DB_PREFIX")."user u2 on u2.id = a.invite_user_id where 1=1 ";
		if($goods_id > 0)
		{
			  $sql .= " and a.goods_id = ".$goods_id;
		}
			  
		if (!empty($sn)){
			$sql .= " and a.sn = '".$sn."'";
		}
		
		if (!empty($user_name)){
			$sql .= " and u1.user_name like '%".$user_name."%'";
		}
		if (!empty($pname)){
			$sql .= " and u2.user_name like '%".$pname."%'";
		}
		//$sql.=" limit ".$limit;
		
		$list = D()->query($sql);
		if($list)
		{
			//dump($sql);
	    	/* csv文件数组 */
	    	$lottery_value = array('id'=>'""', 'goods_id'=>'""', 'goods_name'=>'""', 'sn'=>'""', 'user_name'=>'""', 'mobile_phone'=>'""', 'email'=>'""', 'pname'=>'""','invite_time'=>'""','order_id'=>'""');
			if($page == 1)
	    	$content = utf8ToGB("编号,商品ID,团购名称,抽奖券,会员名称,会员手机号,电子邮件,邀请人,邀请时间,订单号" . "\n");
	    	 
			foreach($list as $k=>$v)
			{
				$v['mobile_phone'] = $v['mobile_phone']; 
				if(fanweC("SYS_ADMIN") != $_SESSION['adm_name'])
				{
					$pattern = "/(\d{4})(\d{3})(\d{4})/";
					$replacement = "\$1***\$3";
					$v['mobile_phone'] = preg_replace($pattern, $replacement, $v['mobile_phone']);
				}
				
				$lottery_value['id'] = utf8ToGB('"' . $v['id'] . '"');
				$lottery_value['goods_id'] = utf8ToGB('"' . $v['goods_id'] . '"');
				$lottery_value['goods_name'] = utf8ToGB('"' . getGoodsNameNoHref($v['goods_id']) . '"');
				$lottery_value['sn'] = utf8ToGB('"' . $v['sn'] . '"');
				$lottery_value['user_name'] = utf8ToGB('"' . $v['user_name'] . '"');
				$lottery_value['mobile_phone'] = utf8ToGB('"' . $v['mobile_phone'] . '"');
				$lottery_value['email'] = utf8ToGB('"' . $v['email'] . '"');
				$lottery_value['pname'] = utf8ToGB('"' . $v['pname'] . '"');
				$lottery_value['invite_time'] = utf8ToGB('"' . toDate($v['invite_time']) . '"');
				$lottery_value['order_id'] = utf8ToGB('"' . M("Order")->where("id=".$v['order_id'])->getField('sn') . '"');				
				$content .= implode(",", $lottery_value) . "\n";
			}	
			
	    	header("Content-Disposition: attachment; filename=lottery_sn_list.csv");
	    	//header("Content-Type: application/octet-stream");
	    	//die();
	    	echo $content;   
		}
	}	
	
}
?>